package com.learn.security.theory.symmetric_encryption;

import com.learn.security.utils.symmetric_encryption.AESUtil;
import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.Security;

/**
 * AES算法（替代DES，应用更广）
 * <p>
 * 应用：移动通信，ssh协议
 */
public class AESTest {

    private static final String encodeStr = "AES test";

    public static void main(String[] args) {

        aseUtil();
        jdkAES();
        bcAES();

    }

    // 用AESUtil实现：AES
    private static void aseUtil() {
        try {
            // 生成KEY
            String key = AESUtil.initKey();
            System.err.println("密钥:" + key);

            // 加密
            byte[] encodeResult = AESUtil.encrypt(encodeStr.getBytes(), key);
            System.err.println("AESUtil aes加密后:" + AESUtil.encryptBASE64(encodeResult));

            // 解密
            byte[] decodeResult = AESUtil.decrypt(encodeResult, key);
            System.err.println("AESUtil aes解密后:" + new String(decodeResult));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 用jdk实现:
    private static void jdkAES() {
        try {
            // 生成KEY
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(128);
            // 产生密钥
            SecretKey secretKey = keyGenerator.generateKey();
            // 获取密钥
            byte[] keyBytes = secretKey.getEncoded();


            // KEY转换
            Key key = new SecretKeySpec(keyBytes, "AES");


            // 加密
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(encodeStr.getBytes());
            System.out.println("jdk aes encrypt:" + Hex.encodeHexString(result));

            // 解密
            cipher.init(Cipher.DECRYPT_MODE, key);
            result = cipher.doFinal(result);
            System.out.println("jdk aes decrypt:" + new String(result));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 用bouncy castle实现:
    private static void bcAES() {
        try {
            Security.addProvider(new BouncyCastleProvider());

            // 生成KEY
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", "BC");
            keyGenerator.getProvider();
            keyGenerator.init(128);
            // 产生密钥
            SecretKey secretKey = keyGenerator.generateKey();
            // 获取密钥
            byte[] keyBytes = secretKey.getEncoded();


            // KEY转换
            Key key = new SecretKeySpec(keyBytes, "AES");


            // 加密
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(encodeStr.getBytes());
            System.out.println("bc aes encrypt:" + Hex.encodeHexString(result));

            // 解密
            cipher.init(Cipher.DECRYPT_MODE, key);
            result = cipher.doFinal(result);
            System.out.println("bc aes decrypt:" + new String(result));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
